<div id="messageContainer">
    <div id="buttonBar">
    	<?php if(CC_FlagFlippers_Manager::isAllowed(null, 'helpdesk', 'new-ticket')) { ?>
            <a class="button altbutton" href="#" id="newEmailButton"><img class="icon16" src="/images/led-ico/email.png" style="" />New Email</a> 
        	<a class="button altbutton" href="#" id="newTicketButton"><img class="icon16" src="/images/led-ico/blog.png" style="" />New Ticket</a>
        <?php }?>
        <?php if(CC_FlagFlippers_Manager::isAllowed(null, 'helpdesk', 'view-rules')) { ?>
        	<a class="button altbutton" href="#" id="viewRules"><img class="icon16" src="/images/led-ico/lightbulb_off.png" style="" />View Mail Rules</a>
    	<?php } ?>
        	<a class="button altbutton" href="#" id="refreshInbox"><img class="icon16" src="/images/fff/arrow_refresh.png" style="" />Refresh Inbox</a>
    </div>

    <div id="messageList">
        <div class="content">
            <table width="100%" class="ticketTable">
                <thead>
                    <tr>
                        <th>From</th>
                        <th>Subject</th>
                        <th>Date</th>
                        <td class="tc last" style="width: 16px">&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                    
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php echo $this->dialogContainer(
    'newTicketModal',
    '<div id="newTicketContent"></div>',
    array(
        'autoOpen'      => false,
        'draggable'     => true,
        'modal'         => true,
        'resizable'     => false,
        'title'         => 'New Ticket',
        'closeOnEscape' => false,
        'width'			=> '800px',
    	'minHeight'     => 550,
        //'position'		=> 'middle',
        'position'		=> 'top',
        'buttons'       => array(
            'Create'	=> new Zend_Json_Expr('function() {
            	document.location=\''.$this->url(array(
                    'module'	 => 'backoffice',
                    'controller' => 'helpdesk',
                    'action'	 => 'index'
                ), "", true) .'\';
                $("#newTicketContent").html(\'\');
            }'),
            'Cancel'    => new Zend_Json_Expr('function() {
                $(this).dialog(\'close\');
                $("#newTicketContent").html(\'\');
            }')
        ),
        'close'			=> new Zend_Json_Expr('function() {
        	$("#newTicketContent").html(\'\');
        }')
    )
); 

echo $this->dialogContainer(
    'ticketModal',
    '<div id="ticketContent"></div>',
    array(
        'autoOpen'      => false,
        'draggable'     => true,
        'modal'         => true,
        'resizable'     => false,
        'title'         => 'Ticket Conversation',
        'closeOnEscape' => false,
        'width'			=> '950px',
        'position'		=> 'top',
        'buttons'       => array(
            'Close'    => new Zend_Json_Expr('function() {
                $("#ticketModal").dialog(\'close\');
            }')
        ),
        'close'			=> new Zend_Json_Expr('function() {
        	$("#ticketContent").html(\'\');
        }')
    )
); 

echo $this->dialogContainer(
    'viewRulesModal',
    '<div id="rulesContent"></div>',
    array(
        'autoOpen'      => false,
        'draggable'     => true,
        'modal'         => true,
        'resizable'     => false,
        'title'         => 'Ticket Rules',
        'closeOnEscape' => true,
        'width'			=> '950px',
        'position'		=> 'top',
        'buttons'       => array(
            'Close'    => new Zend_Json_Expr('function() {
                $("#viewRulesModal").dialog(\'close\');
            }')
        ),
        'close'			=> new Zend_Json_Expr('function() {
        	$("#rulesContent").html(\'\');
        }')
    )
); 

echo $this->dialogContainer(
    'commentTicketModal',
    '<div id="commentTicketContent"></div>',
    array(
        'autoOpen'      => false,
        'draggable'     => true,
        'modal'         => true,
        'resizable'     => false,
        'title'         => 'Add Comment to Ticket Conversation',
        'closeOnEscape' => true,
        'width'			=> '800px',
        'position'		=> 'top',
        'buttons'       => array(
            'Add Comment'    => new Zend_Json_Expr('function() {
            	$("#ticketId").val($("tr.selected").attr("ref"));
            
                $.ajax({  
                    type: "POST",  
                    dataType: "json",
                    url: "/backoffice/helpdesk/new-comment",  
                    data: $("#newCommentForm").serialize(),  
                    success: function(res) {
                    	if(res.success)
                    	{
                    		$("#commentTicketModal").dialog(\'close\');
                    		$("#ticketModal").dialog(\'close\');
                    		$("#commentTicketContent").html(\'\');
                    	}
                    }  
                }); 
            }'),
            'Close'    => new Zend_Json_Expr('function() {
                $("#commentTicketModal").dialog(\'close\');
                $("#commentTicketContent").html(\'\');
            }')
        ),
        'close'			=> new Zend_Json_Expr('function() {
        	$("#commentTicketContent").html(\'\');
        }')
    )
);

echo $this->dialogContainer(
    'esculateTicketModal',
    '<div id="esculateTicketContent"></div>',
    array(
        'autoOpen'      => false,
        'draggable'     => true,
        'modal'         => true,
        'resizable'     => false,
        'title'         => 'Esculate Ticket',
        'closeOnEscape' => true,
        'width'			=> '800px',
        'position'		=> 'top',
        'buttons'       => array(
            'Esculate Ticket'    => new Zend_Json_Expr('function() {
            	$("#ticketId").val($("tr.selected").attr("ref"));
            
                $.ajax({  
                    type: "POST",  
                    dataType: "json",
                    url: "/backoffice/helpdesk/esculate-ticket",  
                    data: $("#esculateTicketForm").serialize(),  
                    success: function(res) {
                    	if(res.success)
                    	{
                    		var folderId = $("#folderId").val();
                    		listFolderTickets(folderId, false);
                    		$("#commentTicketModal").dialog(\'close\');
                    		$("#ticketModal").dialog(\'close\');
                    		$("#commentTicketContent").html(\'\');
                    	}
                    }  
                }); 
            
            
            	$("#esculateTicketModal").dialog(\'close\');
            	$("#esculateTicketContent").html(\'\');
            }'),
            'Close'    => new Zend_Json_Expr('function() {
                $("#esculateTicketModal").dialog(\'close\');
                $("#esculateTicketContent").html(\'\');
            }')
        ),
        'close'			=> new Zend_Json_Expr('function() {
        	$("#esculateTicketContent").html(\'\');
        }')
    )
);
?>

<script>
	$(document).ready(function(){
		$("table.ticketTable").disableSelection();
	});

	$("#newTicketButton").click(function() {
        $("#newTicketContent").html('');
        $("#newTicketModal").parent("div.ui-dialog").css('height', '550px !important');
        $("#newTicketContent").load('/backoffice/helpdesk/new-ticket/ajax/1');
        $("#newTicketModal").dialog('open');
        $("#newTicketModal").parent("div.ui-dialog").css('top', '10px !important');
    });

	$("#refreshInbox").click(function(){
		if($("#folderId").val() != "")
		{
	    	listFolderTickets($("#folderId").val(), false);
		}
	});

	$(".ticketRow").live('click', function(){
		$(".ticketRow").each(function(){
			$(this).removeClass('selected');
		});
		
		$(this).addClass('selected');
		
	});

	$("#viewRules").click(function(){
		$("#rulesContent").html('');
		$("#viewRulesModal").parent("div.ui-dialog").css('height', '550px !important');
		$("#rulesContent").load('/backoffice/helpdesk/view-rules/');
		$("#viewRulesModal").dialog('open');
		$("#viewRulesModal").parent("div.ui-dialog").css('top', '10px !important');
	});

	$(".ticketRow").live('dblclick', function(){ 
	    var ticketId = $(this).attr('ref');
	    $("#ticketContent").html('');
	    $("#ticketContent").load('/backoffice/helpdesk/view-ticket/ticket/'+ticketId);
		$("#ticketModal").dialog('open');
	});
</script>